package LC;

/**
 * https://leetcode.com/problems/minimum-depth-of-binary-tree/description/
 * Given a binary tree, find its minimum depth.
 * The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
 */
public class LC_111_MinimumDepthofBinaryTree_BinaryTree_Depth_Recur {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.right = new TreeNode(4);
        int minDepth = Solution.minDepth(root);
        System.out.println(minDepth);
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Solution {
        static private int min = Integer.MAX_VALUE;
        static private int cur = 0;

        static int minDepth(TreeNode root) {
            depth(root);
            return min;
        }

        private static void depth(TreeNode node) {
            if (node == null) {
                min = cur;
                return;
            }
            cur++;
            if (node.left == null && node.right == null && cur < min) min = cur;
            if (node.left != null) depth(node.left);
            if (node.right != null) depth(node.right);
            cur--;
        }
    }
}